/*
 * SongListTablePanel.java
 *
 * Created on June 20, 2005, 8:27 PM
 */
package com.bff.slimserver.musicjukebox.ui;

import com.bff.slimserver.music.MusicObjects;
import com.bff.slimserver.music.exceptions.PlaylistException;
import com.bff.slimserver.musicjukebox.lib.AlbumTableModel;
import com.bff.slimserver.musicjukebox.lib.ArtistTableModel;
import com.bff.bjj.ui.component.ColorProperties;
import com.bff.slimserver.musicjukebox.lib.GenreTableModel;
import com.bff.slimserver.musicjukebox.ui.components.HideTableColumnListener;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import com.bff.slimserver.musicjukebox.lib.JukeboxProperties;
import com.bff.slimserver.musicjukebox.lib.SearchTableModel;
import com.bff.slimserver.musicjukebox.lib.SongTableModel;
import com.bff.slimserver.musicjukebox.lib.VariousArtistAlbumTableModel;
import com.bff.slimserver.musicjukebox.lib.VariousArtistTableModel;
import com.bff.slimserver.musicjukebox.lib.YearTableModel;
import com.bff.slimserver.musicjukebox.ui.components.BitrateCellRenderer;
import com.bff.slimserver.musicjukebox.ui.components.CoverArtPanel;
import com.bff.slimserver.musicjukebox.ui.components.LengthCellRenderer;
import com.bff.bjj.ui.component.BJJPanel;
import com.bff.slimserver.musicjukebox.ui.components.SongCellRenderer;
import com.bff.slimserver.musicjukebox.ui.components.SongScrollPane;
import com.bff.slimserver.musicjukebox.ui.components.SongTable;
import com.bff.slimserver.musicjukebox.ui.components.SonglistPopupMenu;
import com.bff.slimserver.musicjukebox.ui.components.TrackCellRenderer;
import com.bff.slimserver.musicjukebox.ui.components.savedplaylist.SavedPlaylistDialog;
import com.bff.slimserver.utils.TableSorter;
import com.bff.bjj.utils.Utils;
import java.awt.BorderLayout;
import java.awt.Container;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;
import org.bff.slimserver.SlimFavoritePlugin;
import org.bff.slimserver.SlimPlaylist;
import org.bff.slimserver.SlimSavedPlaylistManager;
import org.bff.slimserver.events.DatabaseScanEvent;
import org.bff.slimserver.events.DatabaseScanListener;
import org.bff.slimserver.exception.SlimConnectionException;
import org.bff.slimserver.exception.SlimException;
import org.bff.slimserver.musicobjects.SlimAlbum;
import org.bff.slimserver.musicobjects.SlimArtist;
import org.bff.slimserver.musicobjects.SlimGenre;
import org.bff.slimserver.musicobjects.SlimPlayableObject;
import org.bff.slimserver.musicobjects.SlimPlaylistItem;
import org.bff.slimserver.musicobjects.SlimSavedPlaylist;
import org.bff.slimserver.musicobjects.SlimSong;

/**
 *
 * @author  Bill
 */
public class SongListTablePanel
        extends BJJPanel
        implements ComponentListener, DatabaseScanListener {

    private SongTableModel tableModel;
    private SlimPlaylist playlist;
    private TableSorter tableSorter;
    private ThumbnailPanel thumbPanel;
    private boolean showingArt;
    private Container parent;
    private JukeboxProperties jp;
    private SlimArtist artist;
    private SlimAlbum album;
    //remove when done
    private JTable infoTable;
    private SlimSavedPlaylistManager playlistManager;
    private SlimFavoritePlugin favoritePlugin;
    private DefaultComboBoxModel groupByModel;

    public SongListTablePanel(boolean showArtOption) {
        this();
        if (!showArtOption) {
            jPanel1.remove(btnRemoveThumb);
            removeThumbnailPane();
        }
    }

    /** Creates new form SongListTablePanel */
    public SongListTablePanel() {
        super();
        jp = JukeboxProperties.getInstance();

        try {
            this.favoritePlugin = jp.getFavoritesPlugin();
        } catch (SlimConnectionException ex) {
            Logger.getLogger(SongListTablePanel.class.getName()).log(Level.SEVERE, null, ex);
        }

        tableSorter = new TableSorter(tableModel);
        parent = this;
        this.showingArt = jp.isShowCoverArt();
        try {
            this.playlist = jp.getPlaylist();
        } catch (PlaylistException ex) {
            Logger.getLogger(SongListTablePanel.class.getName()).log(Level.SEVERE, null, ex);
        }

        infoTable = new SongTable();
        infoTable.setBackground(ColorProperties.PANEL_BACKGROUND);
        infoTable.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_OFF);
        infoTable.setOpaque(false);
        infoTable.addMouseListener(new java.awt.event.MouseAdapter() {

            public void mouseClicked(java.awt.event.MouseEvent evt) {
                infoTableMouseClicked(evt);
            }

            public void mousePressed(java.awt.event.MouseEvent evt) {
                infoTableMousePressed(evt);
            }
        });
        infoTable.addComponentListener(new java.awt.event.ComponentAdapter() {

            public void componentResized(java.awt.event.ComponentEvent evt) {
                infoTableComponentResized(evt);
            }
        });

        initComponents();
        try {
            setPlaylistManager(jp.getPlaylistManager());
        } catch (SlimConnectionException ex) {
            Logger.getLogger(SongListTablePanel.class.getName()).log(Level.SEVERE, null, ex);
        }

        try {
            if (jp.getDatabase().isRescanning()) {
                lblRescan.setVisible(true);
            } else {
                lblRescan.setVisible(false);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        setScrollbarSize();
        addComponentListener(this);

        if (showingArt) {
            createThumbnailPanel();
        } else {
            removeThumbnailPane();
        }

        if (showingArt) {
            btnRemoveThumb.setText("Hide Art");
        } else {
            btnRemoveThumb.setText("Show Art");
        }
        infoTableScrollPane.getViewport().setBackground(ColorProperties.LIST_BACKGROUND_EVEN);

        jp.getSlimMonitor().addDatabaseScanListener(this);

        loadProgressBar.setVisible(false);

        checkButtonAbility();

        panelGroupByFolder.setVisible(false);
    }

    private void createThumbnailPanel() {
        thumbPanel = new ThumbnailPanel();
        thumbPanel.setPreferredSize(new Dimension(300, 300));
        topPanel.add(thumbPanel);

        thumbPanel.addMouseListener(new MouseListener() {

            public void mouseClicked(MouseEvent e) {
                Container comp = parent;
                while (!(comp instanceof JFrame)) {
                    comp = comp.getParent();
                }

                //@TODO Fix this
                AlbumThumbnailDialog dialog = new AlbumThumbnailDialog((JFrame) comp, ((SlimSong) thumbPanel.getSong()).getAlbum());
                dialog.setVisible(true);
            }

            @Override
            public void mousePressed(MouseEvent e) {
            }

            @Override
            public void mouseReleased(MouseEvent e) {
            }

            @Override
            public void mouseEntered(MouseEvent e) {
            }

            @Override
            public void mouseExited(MouseEvent e) {
            }
        });
        add(topPanel, BorderLayout.NORTH);
    }

    public void removeThumbnailPane() {
        try {
            if (thumbPanel != null) {
                topPanel.remove(thumbPanel);
            }
            remove(topPanel);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        btnPanel = new BJJPanel();
        jPanel1 = new BJJPanel();
        btnAddToPlaylist = new com.bff.bjj.ui.component.BJJButton();
        btnPlayNext = new com.bff.bjj.ui.component.BJJButton();
        btnClearAndPlay = new com.bff.bjj.ui.component.BJJButton();
        btnRemoveThumb = new com.bff.bjj.ui.component.BJJButton();
        btnAddSavedPlaylsit = new com.bff.bjj.ui.component.BJJButton();
        btnAddFavorite = new com.bff.bjj.ui.component.BJJButton();
        jPanel2 = new BJJPanel();
        infoTableScrollPane = new SongScrollPane(infoTable);
        jPanel3 = new BJJPanel();
        lblRescan = new javax.swing.JLabel();
        loadProgressBar = new javax.swing.JProgressBar();
        panelGroupByFolder = new BJJPanel();
        jLabel1 = new javax.swing.JLabel();
        cbGroupBy = new javax.swing.JComboBox();
        topPanel = new CoverArtPanel();

        setLayout(new java.awt.BorderLayout());

        btnPanel.setLayout(new java.awt.BorderLayout());

        jPanel1.setLayout(new java.awt.GridBagLayout());

        btnAddToPlaylist.setText("Add to Playlist");
        btnAddToPlaylist.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddToPlaylistActionPerformed(evt);
            }
        });
        jPanel1.add(btnAddToPlaylist, new java.awt.GridBagConstraints());

        btnPlayNext.setText("Play Next");
        btnPlayNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPlayNextActionPerformed(evt);
            }
        });
        jPanel1.add(btnPlayNext, new java.awt.GridBagConstraints());

        btnClearAndPlay.setText("Clear and Play");
        btnClearAndPlay.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClearAndPlayActionPerformed(evt);
            }
        });
        jPanel1.add(btnClearAndPlay, new java.awt.GridBagConstraints());

        btnRemoveThumb.setText("Hide Art");
        btnRemoveThumb.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRemoveThumbActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 5;
        gridBagConstraints.gridy = 0;
        jPanel1.add(btnRemoveThumb, gridBagConstraints);

        btnAddSavedPlaylsit.setText("Add to Saved Playlist");
        btnAddSavedPlaylsit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddSavedPlaylsitActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 3;
        gridBagConstraints.gridy = 0;
        jPanel1.add(btnAddSavedPlaylsit, gridBagConstraints);

        btnAddFavorite.setText("Add to Favorites");
        btnAddFavorite.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddFavoriteActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 0;
        jPanel1.add(btnAddFavorite, gridBagConstraints);

        btnPanel.add(jPanel1, java.awt.BorderLayout.SOUTH);

        add(btnPanel, java.awt.BorderLayout.SOUTH);

        jPanel2.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                jPanel2ComponentResized(evt);
            }
        });
        jPanel2.setLayout(new java.awt.BorderLayout());

        infoTableScrollPane.setBackground(ColorProperties.LIST_BACKGROUND_EVEN);
        infoTableScrollPane.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        infoTableScrollPane.setMaximumSize(new java.awt.Dimension(800, 32767));
        infoTableScrollPane.setPreferredSize(new java.awt.Dimension(200, 200));
        jPanel2.add(infoTableScrollPane, java.awt.BorderLayout.CENTER);

        jPanel3.setLayout(new java.awt.BorderLayout());

        lblRescan.setBackground(ColorProperties.PANEL_BACKGROUND);
        lblRescan.setForeground(ColorProperties.WARNING_MESSAGE_FOREGROUND);
        lblRescan.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("messages"); // NOI18N
        lblRescan.setText(bundle.getString("LABEL_RESCANNING")); // NOI18N
        jPanel3.add(lblRescan, java.awt.BorderLayout.SOUTH);

        loadProgressBar.setIndeterminate(true);
        loadProgressBar.setString("Loading Songs");
        loadProgressBar.setStringPainted(true);
        jPanel3.add(loadProgressBar, java.awt.BorderLayout.NORTH);

        jLabel1.setText("Multiple albums exist");
        panelGroupByFolder.add(jLabel1);

        cbGroupBy.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbGroupBy.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbGroupByItemStateChanged(evt);
            }
        });
        panelGroupByFolder.add(cbGroupBy);

        jPanel3.add(panelGroupByFolder, java.awt.BorderLayout.CENTER);

        jPanel2.add(jPanel3, java.awt.BorderLayout.SOUTH);

        add(jPanel2, java.awt.BorderLayout.CENTER);

        topPanel.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                topPanelComponentResized(evt);
            }
        });
        add(topPanel, java.awt.BorderLayout.NORTH);
    }// </editor-fold>//GEN-END:initComponents

    private void jPanel2ComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_jPanel2ComponentResized
        //infoTableScrollPane.setPreferredSize(jPanel2.getSize());
    }//GEN-LAST:event_jPanel2ComponentResized

    private void btnAddToPlaylistActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddToPlaylistActionPerformed
        this.setCursor(Utils.getHourglassCursor());
        try {
            int rows[] = infoTable.getSelectedRows();

            if (rows.length > 1) {
                List<SlimPlayableObject> list = new ArrayList<SlimPlayableObject>();
                for (int i = 0; i < rows.length; i++) {
                    SlimSong song = tableModel.getSong(tableSorter.modelIndex(rows[i]));
                    list.add(song);
                }
                try {
                    getPlaylist().addItems(list);
                } catch (SlimException ex) {
                    Logger.getLogger(SongListTablePanel.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else {
                try {
                    SlimSong song = tableModel.getSong(tableSorter.modelIndex(rows[0]));
                    getPlaylist().addItem(song);
                } catch (SlimException ex) {
                    Logger.getLogger(SongListTablePanel.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        } finally {
            this.setCursor(Utils.getNormalCursor());
        }
    }//GEN-LAST:event_btnAddToPlaylistActionPerformed

    private void topPanelComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_topPanelComponentResized
        // TODO add your handling code here:
}//GEN-LAST:event_topPanelComponentResized

    private void btnRemoveThumbActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRemoveThumbActionPerformed
        if (showingArt) {
            showingArt = false;
            btnRemoveThumb.setText("Show Art");
            removeThumbnailPane();
        } else {
            btnRemoveThumb.setText("Hide Art");
            showingArt = true;
            createThumbnailPanel();
        }
    }//GEN-LAST:event_btnRemoveThumbActionPerformed

private void btnPlayNextActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPlayNextActionPerformed
    this.setCursor(Utils.getHourglassCursor());
    try {
        SlimSong song = null;
        int rows[] = infoTable.getSelectedRows();

        for (int i = 0; i < rows.length; i++) {
            try {
                song = tableModel.getSong(tableSorter.modelIndex(rows[i]));
                getPlaylist().insertItem(song);
            } catch (SlimException ex) {
                Logger.getLogger(SongListTablePanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    } finally {
        this.setCursor(Utils.getNormalCursor());
    }
}//GEN-LAST:event_btnPlayNextActionPerformed

private void btnClearAndPlayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClearAndPlayActionPerformed
    this.setCursor(Utils.getHourglassCursor());
    try {
        SlimSong song = null;
        int rows[] = infoTable.getSelectedRows();

        if (rows.length > 1) {
            return;
        }

        for (int i = 0; i < rows.length; i++) {
            try {
                song = tableModel.getSong(tableSorter.modelIndex(rows[i]));
                getPlaylist().insertItem(song);
            } catch (SlimException ex) {
                Logger.getLogger(SongListTablePanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    } finally {
        this.setCursor(Utils.getNormalCursor());
    }
}//GEN-LAST:event_btnClearAndPlayActionPerformed

private void btnAddSavedPlaylsitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddSavedPlaylsitActionPerformed

    int rows[] = infoTable.getSelectedRows();
    List<SlimSavedPlaylist> playlists = new ArrayList<SlimSavedPlaylist>(MusicObjects.getSavedPlaylists());
    if (playlists.size() < 1) {
        int choice = JOptionPane.showConfirmDialog(Utils.getParentComponent(this),
                "No saved playlists exists.\nWould you like to create an empty playlist?",
                "Create Playlist?",
                JOptionPane.YES_NO_OPTION);

        if (choice == JOptionPane.YES_OPTION) {
            new SavePlaylistDialog(Utils.getParentComponent(this), true).setVisible(true);
        }
    }

    if (playlists.size() > 0) {
        if (rows.length > 0) {
            JFrame p = Utils.getParentComponent(this);
            SavedPlaylistDialog dlg = new SavedPlaylistDialog(p);
            dlg.setLocationRelativeTo(p);
            dlg.setVisible(true);
            if (dlg.getSelectedPlaylist() != null) {
                if (rows.length > 1) {
                    List<SlimSong> list = new ArrayList<SlimSong>();
                    for (int i = 0; i < rows.length; i++) {
                        SlimSong song = tableModel.getSong(tableSorter.modelIndex(rows[i]));
                        list.add(song);
                    }
                    try {
                        getPlaylistManager().addPlaylistSongs(dlg.getSelectedPlaylist(), list);
                    } catch (SlimException ex) {
                        Logger.getLogger(SongListTablePanel.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else {
                    try {
                        SlimSong song = tableModel.getSong(tableSorter.modelIndex(rows[0]));
                        getPlaylistManager().addPlaylistSong(dlg.getSelectedPlaylist(), song);
                    } catch (SlimException ex) {
                        Logger.getLogger(SongListTablePanel.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        }
    }
}//GEN-LAST:event_btnAddSavedPlaylsitActionPerformed

private void btnAddFavoriteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddFavoriteActionPerformed
    this.setCursor(Utils.getHourglassCursor());
    try {
        SlimSong song = null;
        int rows[] = infoTable.getSelectedRows();

        for (int i = 0; i < rows.length; i++) {
            try {
                song = tableModel.getSong(tableSorter.modelIndex(rows[i]));
                getFavoritePlugin().addFavorite(song);
            } catch (SlimException ex) {
                Logger.getLogger(SongListTablePanel.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    } finally {
        this.setCursor(Utils.getNormalCursor());
    }
}//GEN-LAST:event_btnAddFavoriteActionPerformed

private void cbGroupByItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbGroupByItemStateChanged
    if (tableModel instanceof AlbumTableModel) {
        ((AlbumTableModel) tableModel).filterGroup((String) groupByModel.getSelectedItem());
    }
}//GEN-LAST:event_cbGroupByItemStateChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddFavorite;
    private javax.swing.JButton btnAddSavedPlaylsit;
    private javax.swing.JButton btnAddToPlaylist;
    private javax.swing.JButton btnClearAndPlay;
    private javax.swing.JPanel btnPanel;
    private javax.swing.JButton btnPlayNext;
    private javax.swing.JButton btnRemoveThumb;
    private javax.swing.JComboBox cbGroupBy;
    private javax.swing.JScrollPane infoTableScrollPane;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JLabel lblRescan;
    private javax.swing.JProgressBar loadProgressBar;
    private javax.swing.JPanel panelGroupByFolder;
    private javax.swing.JPanel topPanel;
    // End of variables declaration//GEN-END:variables

    public void setVariousArtist(SlimArtist artist) {
        this.artist = artist;
        try {
            setModel(new VariousArtistTableModel(artist));
        } catch (SlimException ex) {
            Logger.getLogger(SongListTablePanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void setArtist(SlimArtist artist) {
        this.artist = artist;
        try {
            setModel(new ArtistTableModel(artist));

        } catch (SlimException ex) {
            Logger.getLogger(SongListTablePanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void setYear(String year) {
        try {
            setModel(new YearTableModel(year));
        } catch (SlimException ex) {
            Logger.getLogger(SongListTablePanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void setGenre(SlimGenre genre) {
        try {
            setModel(new GenreTableModel(genre));
        } catch (SlimException ex) {
            Logger.getLogger(SongListTablePanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public SlimArtist getArtist() {
        return this.artist;
    }

    public SlimAlbum getAlbum() {
        return this.album;
    }

    public void setVariousAlbum(SlimAlbum album) {
        this.album = album;
        try {
            if (album != null) {
                setModel(new VariousArtistAlbumTableModel(album));
            }
        } catch (Exception ex) {
            Logger.getLogger(SongListTablePanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void setAlbum(SlimAlbum album) {
        this.album = album;
        try {
            if (album != null) {
                setModel(new AlbumTableModel(album));
            }
        } catch (Exception ex) {
            Logger.getLogger(SongListTablePanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void infoTableComponentResized(java.awt.event.ComponentEvent evt) {
        setScrollbarSize();
    }

    private void infoTableMousePressed(java.awt.event.MouseEvent evt) {
        if (SwingUtilities.isRightMouseButton(evt)) {
            int row = infoTable.rowAtPoint(evt.getPoint());
            if (row > -1) {
                ListSelectionModel selectionModel = infoTable.getSelectionModel();
                selectionModel.setSelectionInterval(row, row);
                SlimPlaylistItem song = tableModel.getSong(tableSorter.modelIndex(row));
                SonglistPopupMenu popupMenu = new SonglistPopupMenu(Utils.getParentComponent(this), getPlaylist(), song);
                if (evt.isPopupTrigger()) {
                    popupMenu.show(evt.getComponent(),
                            evt.getX(), evt.getY());
                }

            }
        } else {
            if (infoTable.getSelectedRows().length == 1) {
                SlimSong song = tableModel.getSong(tableSorter.modelIndex(infoTable.getSelectedRow()));
                if (showingArt) {
                    thumbPanel.loadImage(song, JukeboxProperties.getInstance().getImageDelayInMillis());
                }

            } else {
                SlimAlbum album = null;
                boolean albumsMatch = true;
                SlimSong song = null;
                int[] songs = infoTable.getSelectedRows();
                for (int i = 0; i
                        < songs.length; i++) {
                    int songIndex = songs[i];
                    song =
                            tableModel.getSong(tableSorter.modelIndex(songIndex));
                    if (album == null) {
                        album = song.getAlbum();
                    } else {
                        if (!album.equals(song.getAlbum())) {
                            albumsMatch = false;
                        } else {
                            break;
                        }
                    }
                }
                if (albumsMatch && showingArt) {
                    if (thumbPanel == null) {
                        createThumbnailPanel();
                    }
                    thumbPanel.loadImage(song, JukeboxProperties.getInstance().getImageDelayInMillis());
                }
            }
        }
        checkButtonAbility();
    }

    private void infoTableMouseClicked(java.awt.event.MouseEvent evt) {
        if (evt.getClickCount() == 2) {
            try {
                int row = infoTable.rowAtPoint(evt.getPoint());
                SlimSong song = tableModel.getSong(tableSorter.modelIndex(row));
                this.setCursor(Utils.getHourglassCursor());
                getPlaylist().addItem(song);
            } catch (SlimException ex) {
                Logger.getLogger(SongListTablePanel.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                this.setCursor(Utils.getNormalCursor());
            }
        }
    }

    public void setSongs(Collection<SlimSong> songs) {
        setModel(new SearchTableModel(songs));
        checkButtonAbility();
    }

    private void setModel(SongTableModel tableModel) {
        this.tableModel = tableModel;

        JTableHeader tableHeader = infoTable.getTableHeader();
        tableHeader.setBackground(ColorProperties.TABLE_HEADER_BACKGROUND);
        tableHeader.setFont(new Font(tableHeader.getFont().getFontName(), Font.BOLD, tableHeader.getFont().getSize()));
        tableSorter = new TableSorter(tableModel);
        tableSorter.setSortingStatus(tableModel.getSortColumn(), TableSorter.ASCENDING);
        tableSorter.setTableModel(tableModel);
        tableSorter.setTableHeader(infoTable.getTableHeader());

        infoTable.setModel(tableSorter);
        new HideTableColumnListener(infoTable);
        //infoTable.addMouseListener(new HideTableColumnListener())

        if ((tableModel instanceof AlbumTableModel) && jp.isGroupByFolder()) {
            AlbumTableModel model = (AlbumTableModel) tableModel;
            if (model.getGroupList().size() > 1) {
                panelGroupByFolder.setVisible(true);
                groupByModel = new DefaultComboBoxModel();
                for (String s : model.getGroupList()) {
                    groupByModel.addElement(s);
                    cbGroupBy.setModel(groupByModel);
                    model.filterGroup((String) groupByModel.getSelectedItem());
                }
            } else {
                panelGroupByFolder.setVisible(false);
            }

        } else {
            panelGroupByFolder.setVisible(false);
        }

        int width = 0;

        TableColumn tc = infoTable.getColumnModel().getColumn(tableModel.getLengthColumnIndex());
        tc.setCellRenderer(new LengthCellRenderer());
        width +=
                tc.getWidth();

        if (tableModel.getAlbumColumnIndex() > -1) {
            tc = infoTable.getColumnModel().getColumn(tableModel.getAlbumColumnIndex());
            tc.setCellRenderer(new SongCellRenderer());
        }

        if (tableModel.getArtistColumnIndex() > -1) {
            tc = infoTable.getColumnModel().getColumn(tableModel.getArtistColumnIndex());
            tc.setCellRenderer(new SongCellRenderer());
        }

        if (tableModel.getTitleColumnIndex() > -1) {
            tc = infoTable.getColumnModel().getColumn(tableModel.getTitleColumnIndex());
            tc.setCellRenderer(new SongCellRenderer());
        }
        /*
        if(tableModel.getGenreColumnIndex() > -1) {
        tc = infoTable.getColumnModel().getColumn(tableModel.getGenreColumnIndex());
        tc.setCellRenderer(new SongCellRenderer());
        width+=tc.getWidth();
        }
         */

        if (tableModel.getBitrateColumnIndex() > -1) {
            tc = infoTable.getColumnModel().getColumn(tableModel.getBitrateColumnIndex());
            tc.setCellRenderer(new BitrateCellRenderer());
            width +=
                    tc.getWidth();
        }

        if (tableModel.getTrackColumnIndex() > -1) {
            tc = infoTable.getColumnModel().getColumn(tableModel.getTrackColumnIndex());
            tc.setCellRenderer(new TrackCellRenderer());
            width +=
                    tc.getWidth();
        }

        if (tableModel.getYearColumnIndex() > -1) {
            tc = infoTable.getColumnModel().getColumn(tableModel.getYearColumnIndex());
            tc.setCellRenderer(new CenterCellRenderer());
            width +=
                    tc.getWidth();
        }

        setTableSize();
        Utils.autoResizeTable(infoTable, true, 25);
        setTableSize();

        if (showingArt) {
            try {
                thumbPanel.loadImage(tableModel.getSong(tableSorter.modelIndex(0)));
            } catch (Exception e) {
                thumbPanel.removeImage();
            }
        }

        checkButtonAbility();
    }

    private void setTableSize() {
        infoTable.setPreferredSize(new Dimension(this.getWidth() - 0, (int) infoTable.getRowHeight() * infoTable.getRowCount()));
    }

    public SlimPlaylist getPlaylist() {
        return playlist;
    }

    public void setPlaylist(SlimPlaylist playlist) {
        this.playlist = playlist;
    }

    /**
     * @return the playlistManager
     */
    public SlimSavedPlaylistManager getPlaylistManager() {
        return playlistManager;
    }

    /**
     * @param playlistManager the playlistManager to set
     */
    public void setPlaylistManager(SlimSavedPlaylistManager playlistManager) {
        this.playlistManager = playlistManager;
    }

    private void checkButtonAbility() {
        if (infoTable.getSelectedRows().length > 0) {
            setButtonAbility(true);
        } else {
            setButtonAbility(false);
        }

        if (infoTable.getSelectedRows().length == 1) {
            btnClearAndPlay.setEnabled(true);
        } else {
            btnClearAndPlay.setEnabled(false);
        }
    }

    private void setButtonAbility(boolean b) {
        btnAddSavedPlaylsit.setEnabled(b);
        btnAddToPlaylist.setEnabled(b);
        btnPlayNext.setEnabled(b);
        btnAddFavorite.setEnabled(b);
    }

    /**
     * @return the favoritePlugin
     */
    public SlimFavoritePlugin getFavoritePlugin() {
        return favoritePlugin;
    }

    /**
     * @param favoritePlugin the favoritePlugin to set
     */
    public void setFavoritePlugin(SlimFavoritePlugin favoritePlugin) {
        this.favoritePlugin = favoritePlugin;
    }

    private class CenterCellRenderer extends SongCellRenderer {

        CenterCellRenderer() {
            setHorizontalAlignment(SwingConstants.CENTER);
        }
    }

    private void setScrollbarSize() {
        int width = infoTable.getWidth() + 2;
        int height = jPanel2.getHeight() - 5;
        int maxWidth = jPanel2.getWidth() - 5;

        if (infoTable.getHeight() > height && width < maxWidth) {
            width += 18;
        }

        if (width > maxWidth) {
            width = maxWidth;
        }

        infoTableScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        infoTableScrollPane.setPreferredSize(new Dimension(width, height));
        infoTableScrollPane.setSize(new Dimension(width, height));
        jPanel2.revalidate();
    }

    public void componentHidden(ComponentEvent arg0) {
        //dont care
    }

    public void componentMoved(ComponentEvent arg0) {
        //dont care
    }

    public void componentResized(ComponentEvent arg0) {
        setTableSize();
    }

    public void componentShown(ComponentEvent arg0) {
        //dont care
    }

    @Override
    public void databaseScanEventReceived(DatabaseScanEvent event) {
        if (event.getId() == DatabaseScanEvent.SCAN_STARTED) {
            lblRescan.setVisible(true);
        } else {
            lblRescan.setVisible(false);
        }

    }

    public void setProgressBarVisible(boolean visible) {
        loadProgressBar.setVisible(visible);
    }
    /*
    private class RefreshThread implements Runnable {

    @Override
    public void run() {
    while (true) {
    try {
    if (!jp.getDatabase().isRescanning()) {
    lblRescan.setVisible(false);
    } else {
    lblRescan.setVisible(true);
    }

    try {
    Thread.sleep(60 * 1000);
    } catch (InterruptedException ie) {
    ie.printStackTrace();
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
    }
     */
}
